package com.thread.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;

public class ArrayBlockingQueueTest {

    public static void main(String[] args) {
        BlockingQueue<Integer> idQueue = new DelayQueue();
        //这里先有一个线程从队列中获取值
        new Thread(() -> {
            for(;;){
                try {
                    Integer value = idQueue.take();
                    idQueue.poll(2, TimeUnit.SECONDS);
                    System.out.println("消费者从队列中获取到值：" + value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //这里有一个线程每隔 1 秒往队列中放
        new Thread(() -> {
            for(int i=0; i < 5; i++){
                try {
                    //往队列中插入
                    idQueue.put(i);
                    System.out.println("生产者往队列中放入值：" + i);
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
